//
//  SZBusAppDelegate.m
//  SZBus
//
//  Created by Kengi Chan on 10/13/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "SZBusAppDelegate.h"
#import "Route.h"
#import "Station.h"

@implementation SZBusAppDelegate

@synthesize window;
@synthesize tabBarController;

@synthesize routeArray;


- (void)applicationDidFinishLaunching:(UIApplication *)application {
    
	[self copyDatabaseIfNeeded];
	self.routeArray = [[NSMutableArray alloc] init];	
	[Route getInitialDataToDisplay:[self getDBPath]];	
	
    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];
	[window makeKeyAndVisible];
}

- (void)applicationWillTerminate:(UIApplication *)application {
	
	[Route finalizeStatements];
	
}

- (void)dealloc {
	[routeArray release];
	
    [tabBarController release];	
    [window release];
    [super dealloc];
}

- (void) copyDatabaseIfNeeded {
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSError *error;
	NSString *dbPath = [self getDBPath];
	BOOL success = [fileManager fileExistsAtPath:dbPath];
	if(!success) {
		
		NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kdbFilename];
		success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
		
		if (!success) 
			NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
	}
}

- (NSString *) getDBPath {
	/*
	NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kdbFilename];
	return defaultDBPath;
	*/
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
	NSString *documentsDir = [paths objectAtIndex:0];
	return [documentsDir stringByAppendingPathComponent:kdbFilename];
	
}

/*
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController {
}
*/

/*
- (void)tabBarController:(UITabBarController *)tabBarController 
 didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}
*/




@end

